// 项目的JS入口文件
import Vue from 'vue'

// 路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// vuex
import Vuex from 'vuex'
Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem("car") || '[]')

var store = new Vuex.Store({
  state: {
      car: car //将购物车中的商品数据存起来，存储一些商品的对象{id: count: price: selected:是否被选中}
  },
  mutations: {
      addToCar(state,goodsinfo) {

        var flag = false;//假设没有找到对应商品
        //some是找
        state.car.some(item => {
          if(item.id == goodsinfo.id) {
            item.count += parseInt(goodsinfo.count);
            flag =true;
            return true;
          } 
        })
        if(!flag){
          state.car.push(goodsinfo);
        }
        //当更新car之后，把car存储到本地的localStorage中，不然每次刷新页面都是一块暂时存储，所以之前的会请空
        
        localStorage.setItem('car', JSON.stringify(state.car))
      },
      //修改购物车商品数量值
      updateGoodsInfo(state,goodsinfo) {
        state.car.some(item => {
          if(item.id == goodsinfo.id){
            item.count = parseInt(goodsinfo.count);
            return true;
          }
        })
        localStorage.setItem('car', JSON.stringify(state.car))
      },
      removeFromCar(state,id){
        // 根据id从car中删除对应的数据
        state.car.some((item,i) => {
          if(item.id == id) {
            state.car.splice(i,1);
            return true;
          }
        })
        localStorage.setItem('car', JSON.stringify(state.car));
      },
      updateGoodsSelected(state,info){
        state.car.some(item => {
          if(item.id == info.id){
            item.selected = info.selected;
            return true;
          }
        })
        localStorage.setItem('car', JSON.stringify(state.car));
      }
    
  },
  getters: {
      // 这里是用作计算computed的功能
      getAllCount(state) {
        var c = 0;
        state.car.forEach(item => {
          c+=item.count;
        })
        return c;
      },
      getGoodsCount(state) {
        var o = {};
        state.car.forEach(item => {
          o[item.id] = item.count;
        })
        return o;
      },
      getGoodsSelected(state) {
        var o = {}
        state.car.forEach(item => {
          o[item.id] = item.selected;
        })
        return o;
      },
      getGoodsCountAndAmount(state) {
        var o = {
          count: 0,
          amount: 0
        };
        state.car.forEach(item => {
          if(item.selected){
            o.count += item.count;
            o.amount += item.count*item.price;
          }
        })
        return o;
      }
     
  }

})



// 用于格式化时间
import moment from 'moment'

import VueResource from 'vue-resource'

Vue.filter('dataFormat',function(dataStr,pattern='YYYY-MM-DD HH:mm:ss'){
 return moment(dataStr).format(pattern)

})

Vue.use(VueResource)
// 设置请求的根路径
Vue.http.options.root = 'http://www.liulongbin.top:3005';
// http://www.liulongbin.top:3005
Vue.http.options.emulateJSON = true;

import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

// import { Header,Swipe, SwipeItem,Button,Lazyload } from 'mint-ui';

// Vue.use(Lazyload);
// Vue.component(Header.name, Header); 
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)

// 安装图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)



import router from './router.js'
import app from './App.vue'



var vm = new Vue({
  el: '#app',
  render: c => c(app),
  router,
  store
})
